<?php
/*
 * 商用车信息网
 * 检查表
 *      xd_xxx,
 *      xd_xxx_data
 *      xx/201307/31/
 *      xxx/SpiderResult.db3
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 6e4acc1ccb5effdcebb7c4717cdea1068dc7b0ec $
 */
set_time_limit(0);
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setAim('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$hd->setSource('sqlite:d:/caiji/386/SpiderResult.db3', '', '');
$hd->setSourceInfo('content', 'id', '');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 1,
	'interval'  => 0,
	'debug'     => '',
	'moduleid'  => 26, // 资讯 26 导购23
	'catid'     => 2562, // 新闻  ~ 资讯>房车[2564], 新能源车-2563, 商用车-2562, 乘用车-2561
	'picAdd'    => 'http://www.cvehicles.com.cn/',
	'siteName'  => '中国商用车信息网',
	'userid'    => '1',
	'username'  => 'xddlmaster', // id
	'timeType'  => 2, // 1 2013-07-17 10:07:56  2 2013-07-17
);
$fields = array(
	'time'     => 'time', // 时间字段
	'title'    => 'title', // 标题字段
	'id'       => 'ID', // id
	'copyfrom' => 'comefrom', // from
);
$hd->initParams($params);

$sql = 'select * from ' . $hd->getSourceTable() . ' where id>' . $hd->getParam('id') . ' and 已发=0 order by id ASC limit ' . $hd->getParam('limit');

$result = $hd->getDbSource()->query($sql);
$rs     = $result->fetchAll();

if (!$rs) {
	$hd->times();
	$hd->end();
} else {
	$carFolders = array();
	foreach ($rs as $k => $v) {
		$insert = array();
		$hd->setParam('id', $v[$fields['id']]); // set id

		// set handler over
		$sqlUpdate = Sql::update($hd->getSourceTable(), '已发 =1', "id={$v[$fields['id']]}");

		// filter time
		if (!AutoImport::checkTime($v[$fields['time']])) {
			$hd->getDbSource()->query($sqlUpdate);
			var_dump('时间不匹配-跳过');
			continue;
		}

		$ids     = AutoImport::match($v[$fields['title']]);
		$time    = AutoImport::getTime($v['time'], $hd->getParam('timeType'));
		$content = $v['content'];

		if (strrpos($content, '[pagebreak]') !== false) {
			$content = substr($content, 0, strrpos($content, '[pagebreak]'));
		}

		$content             = Post::clearLink($content);
		$content             = AutoImport::fullUrl($content, 'jpg|jpeg|gif|png|bmp', $hd->getParam('picAdd'));
		$thumb               = AutoImport::saveThumb($content, 1, 150, 113, $hd->getParam('moduleid'), $hd->getParam('userid'), $time, $hd->getParam('picAdd'));
		$insert['title']     = Str::addSlashes($v[$fields['title']]);
		$insert['subtitle']  = '';
		$insert['arbid']     = $ids[0];
		$insert['catid']     = $hd->getParam('catid');
		$insert['armid']     = $ids[1];
		$insert['arcid']     = $ids[2];
		$insert['introduce'] = Str::addSlashes(Str::cut(strip_tags($content), 120));
		$insert['copyfrom']  = $v[$fields['copyfrom']] ? $v[$fields['copyfrom']] : $hd->getParam('siteName');
		$insert['fromurl']   = '';
		$insert['hits']      = rand(20, 5000);
		$insert['thumb']     = $thumb;
		$insert['username']  = $hd->getParam('username');
		$insert['addtime']   = $time;
		$insert['editor']    = $hd->getParam('username');
		$insert['edittime']  = $time;
		$insert['ip']        = '60.208.115.170';
		$insert['status']    = 3;
		$insert['islink']    = 0;
		if (empty($content)) {
			exit('Error: Empty Content! ON File:"'.__FILE__.'" ON Line:'.__LINE__);
		}
		$sql = Sql::insert('xd_information', $insert);
		if ($hd->getDbAim()->query($sql)) {
			$insertId                 = $hd->getDbAim()->lastInsertId();
			$insertContent            = array();
			$content                  = AutoImport::saveRemote($content, 'jpg|jpeg|gif|png|bmp', $hd->getParam('moduleid'), $hd->getParam('userid'), $time, $hd->getParam('picAdd'));
			$insertContent['itemid']  = $insertId;
			$insertContent['content'] = Str::addSlashes($content);
			$sql                      = Sql::insert('xd_information_data', $insertContent);
			if ($hd->getDbAim()->query($sql)) {
				$hd->getDbSource()->query($sqlUpdate);
				var_dump($hd->times());
				var_dump($v[$fields['id']], $v[$fields['title']], $v[$fields['time']]);
			} else {
				exit($sql);
			}
		} else {
			exit($sql);
		}
	}

}
var_dump($hd->getParams());
echo $hd->start($hd->getParams());